//
// Created by guanyicheng on 2022/6/14.
//

#include "../../common.h"

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {

        if (root == nullptr) return 0;
        int maxDept = 0;
        deque<TreeNode*> dp{};
        dp.emplace_back(root);
        while (!dp.empty()) {
            int size = dp.size();
            maxDept++;
            while (size-- > 0) {
                auto t = dp.front();
                dp.pop_front();
                if (t->left != nullptr) {
                    dp.emplace_back(t->left);
                }
                if (t->right != nullptr) {
                    dp.emplace_back(t->right);
                }
            }
        }
        return maxDept;
    }
};